//
//  KlineSelectButton.swift
//  Exchange
//
//  Created by rui on 2018/4/28.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

enum KlineSelectButtonType: Int {
    case normal = 0,up = 1,down = 2
}

class KlineSelectButton: UIView {
    
    var buttonType: KlineSelectButtonType = .normal
    
    @IBInspectable var title: String? {
        get {
            return btn.titleLabel?.text
        }
        set {
            if let subStrings = newValue?.split(separator: "|") {
                if subStrings.count >= 2,let type = subStrings.last {
                    let typeStr = String(type)
                    buttonType = KlineSelectButtonType(rawValue: Int(typeStr)!)!
                }
                let title = String(subStrings.first!)
                btn.setTitle(title, for: .normal)
            }
        }
    }
    
    private var arrowLayer: CAShapeLayer?

    typealias Click = (KlineSelectButton)->()
    
    var click: Click?
    
    
    var isSelected = false {
        didSet {
            btn.isSelected = isSelected
            indicator.isHidden = !isSelected
        }
    }
    
    private var btn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        btn.themeSetTitleColor(ThemeManager.ThemeColorKeys.text3, for: .normal)
        btn.setTitleColor(kThemeColor, for: .selected)
        btn.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        return btn
    }()
    
    private var indicator: UIView = {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: 0, height: 2)
        view.layer.cornerRadius = 1
        view.layer.masksToBounds = true
        view.backgroundColor = kThemeColor
        view.isHidden = true
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initUI()
    }
    
    func initUI() {
        self.addSubview(btn)
        self.addSubview(indicator)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        btn.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height - 2)
        indicator.frame = CGRect(x: self.frame.size.width * 0.25, y: self.frame.size.height - 2, width: self.frame.size.width * 0.5, height: 2)
         addArrow()
    }
    
    func addArrow() {
        if buttonType != .normal && arrowLayer == nil {
            let path = UIBezierPath()
            let buttonTitle = self.title ?? ""
            let size = buttonTitle.boundingRect(
                with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude),
                options: NSStringDrawingOptions.usesLineFragmentOrigin,
                attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 12)],
                context: nil).size
            let x = (self.btn.width + size.width) * 0.5 + 2
            if buttonType == .down {
                let y = (self.btn.height + size.height) * 0.5
                path.move(to: CGPoint(x: x, y: y))
                path.addLine(to: CGPoint(x: x + 5, y: y))
                path.addLine(to: CGPoint(x: x + 5, y: y-5))
            }else if buttonType == .up {
                let y = (self.btn.height - size.height) * 0.5
                path.move(to: CGPoint(x: x, y: y))
                path.addLine(to: CGPoint(x: x + 5, y: y))
                path.addLine(to: CGPoint(x: x + 5, y: y+5))
            }
            let arrowlayer = CAShapeLayer()
            arrowlayer.path = path.cgPath
            arrowlayer.fillColor = kLineMoreColor.cgColor
            arrowLayer = arrowlayer
            arrowLayer?.frame = self.bounds
            self.layer.addSublayer(arrowlayer)
        }
    }
    
    @objc func btnClick() {
        click?(self)
    }
}
